import React, {Component} from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  StyleSheet
} from 'react-native';

import Config from '../../config';
import Colors from '../../common/Colors';
import CommonStyles from '../../common/CommonStyles';
import SizeUtils from '../../common/SizeUtils';
import TImage from '../../components/TImage';
import WH_Constants from './WH_Constants';
import CommonListPage from '../common/CommonListPage';
import ToastUtils from '../../common/ToastUtils';
import WH_PostItemView from './WH_PostJDZItemView';
import WH_RequestPostsAction from '../../actions/workhard/WH_RequestPostsAction';
import WH_RequestRecommendAction from '../../actions/workhard/WH_RequestRecommendAction'
import CommonCheckUserTypeAction from '../../actions/common/CommonCheckUserTypeAction';

const BUTTON_ARR =[
  {key:"zhinan",data:Config.pages.workHard_zhinan, name:"使用指南", icon:require("../../../resource/images/workhard/zhinan.png")},
  {key:"fenlei",data:Config.pages.workHard_alltype, name:"全部分类", icon:require("../../../resource/images/workhard/fenlei.png")},
  {key:"huiyinbi", name:"回音壁",data:Config.pages.workHard_huiyinbi,icon:require("../../../resource/images/workhard/huiyinbi.png")},
  {key:"huojiang",data:Config.pages.workHard_winner, name:"获奖情况", icon:require("../../../resource/images/workhard/huojiang.png")},
	{key:'TOP10',data:Config.pages.wh_type_jdzrank, name:"排行榜", icon:require("../../../resource/images/workhard/paihangbang.png")},
];
const SORT_ARR=[
  {key:'1',name:'最新'},
  {key:'3',name:'点赞数排序'},
  {key:'4',name:'评论数排序'},
  {key:'2',name:'点击量排序'},
];
const USERURL ='http://43.241.76.189:8082/admin/app/UserPostGuide.html';
export default class WH_JindianziMainView extends CommonListPage {
  constructor(props) {
    super(props);
    this.select_index=0;
    this._dataKey = "post_id";
  }
  componentDidMount() {
    new WH_RequestRecommendAction().call(this, {sort_id:1,
      page:1, page_size:1,recommend:1});
  }
  _dealEnterPage() {
    super._dealEnterPage();
    new WH_RequestRecommendAction().call(this, {sort_id:1,
      page:1, page_size:1,recommend:1});
  }
  _renderTopView(){
    return(
      <View>
        <View style={{
            flexDirection:"row",
              marginTop:SizeUtils.translateDesignHeightToCode(10),
              marginBottom:SizeUtils.translateDesignHeightToCode(20),
              justifyContent:"space-around",
              alignItems: "center"}}>
              {BUTTON_ARR.map((item,index)=> this._getPageButton(item) )}
        </View>
        <View style={{
            height:SizeUtils.translateDesignHeightToCode(20),
            backgroundColor:'#eee'}}/>
      </View>
    );
  }
  _onRenderHeader(){
    if (this._listView) {
      const stateData =this._listView.state;
      if (stateData.data.length&&(stateData.data.length>0||stateData.viewStatus===3)) {
        return(
        <View>
          <View style={{
            height:SizeUtils.translateDesignHeightToCode(80),
            flexDirection:'row',
            alignItems: 'center',
            backgroundColor:'#f8f8f8'}}>
            <Text style={[CommonStyles.secondaryText,
              {color:'#000',
              marginLeft:SizeUtils.translateDesignWidthToCode(30),
              fontSize:SizeUtils.translateFontSizeToCode(30),}]}>
              {'帖子总数: '}
              <Text style={{color:'#ee3535'}}>{stateData.total||0}</Text>
              {'    '}
              {'建议人总数: '}
              <Text style={{color:'#ee3535'}}>{stateData.total_people||0}</Text>
            </Text>
          </View>
          <View style={{
              flexDirection:"row",
              borderColor:'#eeeeee',
              borderBottomWidth:SizeUtils.translateDesignHeightToCode(3),
              height:SizeUtils.translateDesignHeightToCode(80),
              justifyContent:"space-around",
            }}>
              {SORT_ARR.map((item,index)=> this._getSortButton(item,index) )}
          </View>
           {
            this.state.rdata&&this.state.rdata.length>0?
            <TouchableOpacity style={{height:80,paddingLeft:15,marginRight:15,}} onPress={()=>{
                  this._onPressItem(this.state.rdata[0])
                }}>
                <View style={{marginTop:15,flexDirection:'row',justifyContent: 'space-between', width:SizeUtils.getWindowSize().width-30}}>
                    <Text style={[CommonStyles.secondaryText,{color:'#ff6600',fontWeight:'bold',fontSize:16}]}>{'[专家推荐]'}</Text>
                    <TouchableOpacity style={{height:30,width:64}} onPress={()=>{this.gotoPage(Config.pages.wh_zjtj_jdz);}}>
                      <Text style={[CommonStyles.secondaryText,{fontSize:16}]}>{'更多>>'}</Text>
                    </TouchableOpacity>
                </View>
                <Text style={[CommonStyles.secondaryText,{marginTop:10,color:'#000',fontWeight:'bold',fontSize:16}]}>{this.state.rdata[0].title}</Text>
            </TouchableOpacity>:null
           }
           {
            this.state.rdata&&this.state.rdata.length>0?
            <View style={{
            height:SizeUtils.translateDesignHeightToCode(20),
            backgroundColor:'#eee'}}/>:null
          }
        </View>
        ); 
    }
    }	 	
  }
  _onPressItem(data) {
    this.gotoPage(Config.pages.wh_content, {...data});
  }
  _renderBottomView(){
    return(<View style={{width:SizeUtils.getWindowSize().width,
        backgroundColor:'#ff0',flexDirection:'row',
        height:SizeUtils.translateDesignHeightToCode(110)}}>
        <TouchableOpacity activeOpacity={0.85} style={{flex:1,backgroundColor:'#ffc007',
          alignItems: 'center',justifyContent: 'center',}}
          onPress={this._onPressWriteButton.bind(this)}>
          <Text style={{color:'#fff',fontSize:SizeUtils.translateFontSizeToCode(34)}}>
            {'发布金点子'}
          </Text>
        </TouchableOpacity>
        <View style={{backgroundColor:'#fff',width:1}}/>
        <TouchableOpacity activeOpacity={0.85} style={{flex:1,backgroundColor:'#ee3535',
          alignItems: 'center',justifyContent: 'center',
          }}
          onPress={this._onPressGongJiangButton.bind(this)}>
          <Text style={{color:'#fff',fontSize:SizeUtils.translateFontSizeToCode(34)}}>
            {'专家管理'}
          </Text>
        </TouchableOpacity>
      </View>
    )
  }
   _getSortButton(typeData,index) {
    return (
      <TouchableOpacity
        key={typeData.key}
        onPress = {() => {
          if (this.select_index!==index) {
            // this.setState({t:'df'});
            this.select_index=index;
            // this.clear();
            this.doRefresh();
          }
        }}
        style={[{
          borderColor:'#eeeeee',
          flex:1,
          alignItems: 'center',
          justifyContent: 'center',
          borderRightWidth:SizeUtils.translateDesignWidthToCode(3)
        }]}>
          <Text style={[CommonStyles.secondaryText,
              {textAlign:"center",color:index===this.select_index?'#ee3535':'#000',
              fontSize:SizeUtils.translateFontSizeToCode(30),
              backgroundColor:"#00000000", borderWidth:0,}]}>{typeData.name}
          </Text>
      </TouchableOpacity>
    )
  }
  _getPageButton(typeData) {
    return (
      <TouchableOpacity
      	key={typeData.key}
        onPress = {() => this._onPressPageBtn(typeData.key,typeData.data)}
        style={styles.pageBtn}>
        	<TImage source={typeData.icon} style={{width:100,
          		height:100}} />
        	<Text style={[CommonStyles.secondaryText,
          		{textAlign:"center", fontSize:SizeUtils.translateFontSizeToCode(24), backgroundColor:"#00000000", borderWidth:0,}]}>{typeData.name}
        	</Text>
      </TouchableOpacity>
    )
  }
  _onPressPageBtn(key,data){
    if (key==='zhinan') {
       const userData = this.props.pageData.routeData.userData;
       if (userData.perms&&userData.perms.indexOf('s')>=0) {//内部评审权限
          this.gotoPage(data)
       }else{//进入用户发帖指南
        this.gotoPage(Config.pages.post_common_web, {url:USERURL,title:'发帖指南'});
       }
    }else if (key==='huojiang') {
      const userData = this.props.pageData.routeData.userData;
      if (userData.perms&&userData.perms.indexOf('s')>=0) {//内部评审权限
          this.gotoPage(data)
       }else{
        ToastUtils.show("没有部门管理员权限");
       }
    }else{
      this.gotoPage(data)
    }
    
  }
  refRefresh(){
    this.doRefresh();
  }
  _onRenderItem(data, index) {
    return (<WH_PostItemView refRefresh={this.refRefresh.bind(this)} data={data} index={index} showNum = {true} />)
  }
  _getListViewStyle() {
    return {
        width:SizeUtils.getWindowSize().width,
        height:(SizeUtils.getWindowSize().height - Config.paddingTop - SizeUtils.translateDesignHeightToCode(300)),
        backgroundColor:"#ffffff00"};
  }
  _requestData(page, listView) {
    //排序方式，1最新，2最热, 3精选（72小时内的帖子点赞）
    new WH_RequestPostsAction().call(listView, {sort_id:SORT_ARR[this.select_index].key,
      page:page, page_size:10});
  }
  _onPressWriteButton() {
    new CommonCheckUserTypeAction().call(this, {needLogin:true, needVerify:true}, this._onCheckIsUserCallback.bind(this))
  }
  _onPressGongJiangButton() {
    new CommonCheckUserTypeAction().call(this, {needLogin:true, needVerify:true}, this._onCheckIsUserCallGongback.bind(this))
  }
  _onCheckIsUserCallGongback(result) {
    const userData = this.props.pageData.routeData.userData;
      if (userData.perms.indexOf('s')>=0) {//内部评审权限
          this.gotoPage(Config.pages.workHard_zhuanjia);
      }else{
        ToastUtils.show("没有部门管理员权限");
      }
  }
  _onCheckIsUserCallback(result) {
    if(result) {
      this.gotoPage(Config.pages.workHard_write);
    }
  }
}

 const styles = StyleSheet.create({
     pageBtn: {
      flexDirection: 'column',
      alignItems: 'center',
     }
 });

